/*================================================================================
*
*
*                                   
*
*
* 项目名称: mcp2515-spi-can-demo
* 文件名称: CommCAN.c
* 创建日期: 21-03-30
* 文件描述: 
*
================================================================================*/
#include "main.h"
#include "ModbusTable.h"
#include "ModbusSlave.h"
#include "DBG_PR.h"
#include "can.h"
#include "cmsis_os2.h"
#include "CommCAN.h"

//-private data------------------------------------------------------------------
static uint8_t s_flagRxOk;

/********************************************************************************
*函数功能: 
      配置CAN屏蔽器过滤器
*修改记录: 
*日期:                作者:                摘要:
      21-04-18             YummoShin            Create
********************************************************************************/
void InitCanComm(void)
{
    CAN_FilterTypeDef RgbMsgFlt;

    RgbMsgFlt.FilterActivation = ENABLE;
    RgbMsgFlt.FilterBank = 0;
    RgbMsgFlt.FilterFIFOAssignment = CAN_FILTER_FIFO0;
    RgbMsgFlt.FilterIdHigh = 0; //
    RgbMsgFlt.FilterIdLow = 0; //
    RgbMsgFlt.FilterMaskIdHigh = 0;
    RgbMsgFlt.FilterMaskIdLow = 0;
    RgbMsgFlt.FilterMode = CAN_FILTERMODE_IDMASK;
    RgbMsgFlt.FilterScale = CAN_FILTERSCALE_16BIT;
    RgbMsgFlt.SlaveStartFilterBank = 14;
    HAL_CAN_ConfigFilter(&hcan1, &RgbMsgFlt);

    HAL_CAN_Start(&hcan1);
    HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
}

/********************************************************************************
*函数功能: 
*修改记录:
*日期:                             作者:                             摘要:
      Oct 3, 2022      YummoShin         Create
********************************************************************************/
void taskCommCAN(void)
{
  CAN_TxHeaderTypeDef TxHeader;
  uint8_t u8Data[8];
  uint32_t u32TxMailBox;

  while(1)
  {
    if(1==s_flagRxOk)
    {
      s_flagRxOk = 0;
      HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_11);
    }
    if(1==g_flagModbusValChange)
    {
      g_flagModbusValChange = 0;
      TxHeader.StdId = 0x02;
      TxHeader.ExtId = 0x01;
      TxHeader.RTR = CAN_RTR_DATA;
      TxHeader.IDE = CAN_ID_EXT;
      TxHeader.DLC = 4;
      u8Data[0] = g_u16ColorRedtoSet;
      u8Data[1] = g_u16ColorGreentoSet;
      u8Data[2] = g_u16ColorBluetoSet;
      u8Data[3] = g_u16LuminancetoSet;
      HAL_CAN_AddTxMessage(&hcan1, &TxHeader, u8Data, &u32TxMailBox);
    }
    osDelay(10);
  }
}

/********************************************************************************
*函数功能:
    CAN接收中断,获取RGB LED状态数据
*修改记录: 
*日期:                作者:                摘要:
      21-04-18             YummoShin            Create
********************************************************************************/
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
    CAN_RxHeaderTypeDef RgbMsgHeader;
    uint8_t u8Data[8];

    if(hcan==&hcan1)
    {
        HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &RgbMsgHeader, u8Data);
        g_u16ColorRed = u8Data[0];
        g_u16ColorGreen = u8Data[1];
        g_u16ColorBlue = u8Data[2];
        g_u16Luminance = u8Data[3];
        g_u16LedState = u8Data[4];

        s_flagRxOk = 1;
    }
}

